package com.anolesoft.PlanOrder.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.PlanOrder.dto.MesBzzFcfhjhDto;
import com.anolesoft.PlanOrder.vo.MesBzzFcfhjhVo;
import com.anolesoft.Planexecution.entity.MesBzzOrderDo;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface FengCunFangHuMapper {

    @SelectProvider(type= FengCunFangHuMapper.createSQL.class, method = "queryMesGjgyxxFkszSQL")
    List<MesBzzFcfhjhVo> selectPageList(MesBzzFcfhjhDto mesBzzFcfhjhDto);
    public static class createSQL {
        public static String queryMesGjgyxxFkszSQL(MesBzzFcfhjhDto mesBzzFcfhjhDto){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" a.OBJUID," +
                            " a.QUALIFIED_LABEL_NUMBER,"  +
                            " to_char(a.EXPIRATION_TIME,'YYYY-MM-DD') EXPIRATION_TIME,"  +
                            " a.PLAN_TYPE,"  +
                            " a.SEALING_METHOD,"  +
                            " a.INTERNAL_DIVISION_OF_LABOR,"  +
                            " a.SCHEDULED_START_TIME,"  +
                            " a.PLANNED_ASSESSMENT_TIME,"  +
                            " a.SEAL_TYPE,"  +
                            " a.STATE,"  +
                            " a.DELIVERY_TIME,"  +
                            " a.EX_WAREHOUSE_PERSON,"  +
                            " a.WAREHOUSING_TIME,"  +
                            " a.WAREHOUSER,"  +
                            " se7.name C_USER,"  +
                            " a.C_TIME,"  +
                            " a.FACTORY,"  +
                            " a.DEL_FLAG,"  +
                            " a.MODIFIER,"  +
                            " a.MODIFICATION_TIME,"  +
                            " a.WANCHENGREN,"  +
                            " a.WANCHENGSHIJIAN,"  +
                            " a.QUHUOREN,"  +
                            " a.QUHUOSHIJIAN,"  +
                            " a.WANJIANREN,"  +
                            " a.WANJIANSHIJIAN,"  +
                            " a.RUKUREN,"  +
                            " a.RUKUSHIJIAN,"  +
                            " a.WEIHUREN,"  +
                            " a.WEIHUSHIJIAN,"  +
                            " a.PAIGONGHAO,"  +
                            " a.JIXING,"  +
                            " a.TUHAO,"  +
                            " a.JIAOFUCHEJIAN,"  +
                            " a.JIESHOUCHEJIAN,"  +
                            " a.JIAOFUSHUNXU,"  +
                            " a.DW," +
                            " a.MINGCHENG," +
                            " a.SHULIANG, se5.name paigongren, a.PAIGONGSHIJIAN, to_char(a.FANGHU_TIME,'YYYY-MM-DD') FANGHU_TIME," +
                            " se.NAME as WANCHENGRENNAME,"  +
                            " se1.NAME as QUHUORENNAME,"  +
                            " se2.NAME as WANJIANRENNAME,"  +
                            " nvl(se6.NAME,se3.NAME) as RUKURENNAME,"  +
                            " se4.NAME as WEIHURENNAME"  +
                            " FROM  MES_BZZ_FCFHJH a " +
                            " left join stf_employee se on se.objuid = a.wanchengren " +
                            " left join stf_employee se1 on se1.objuid = a.quhuoren " +
                            " left join stf_employee se2 on se2.objuid = a.wanjianren " +
                            " left join stf_employee se3 on se3.employee_code = a.rukuren and se3.factory_id = a.factory" +
                            " left join stf_employee se4 on se4.objuid = a.weihuren " +
                            " left join stf_employee se5 on se5.objuid = a.paigongren " +
                            " left join stf_employee se6 on se6.objuid = a.rukuren " +
                            " left join stf_employee se7 on se7.objuid = a.c_user " +
                            " where a.del_flag = 1 and a.FACTORY = #{factory,jdbcType=VARCHAR}") ;
                    if(!StrUtil.isEmptyOrUndefined(mesBzzFcfhjhDto.getQualifiedLabelNumber())){
                        String qualifiedLabelNumber = mesBzzFcfhjhDto.getQualifiedLabelNumber();
                        sql.append(" and upper(a.QUALIFIED_LABEL_NUMBER) = trim(upper(#{qualifiedLabelNumber, jdbcType=VARCHAR}))");
                    }
                    if(mesBzzFcfhjhDto.getPlanType()!=null&&!String.valueOf(mesBzzFcfhjhDto.getPlanType()).equals("")){
                        int planType = mesBzzFcfhjhDto.getPlanType();
                        sql.append(" and a.PLAN_TYPE = #{planType, jdbcType=INTEGER}");
                    }
                    if(mesBzzFcfhjhDto.getSealType()!=null&&!String.valueOf(mesBzzFcfhjhDto.getSealType()).equals("")){
                        int sealType = mesBzzFcfhjhDto.getSealType();
                        sql.append(" and a.SEAL_TYPE = #{sealType, jdbcType=INTEGER}");
                    }
                    if(mesBzzFcfhjhDto.getState()!=null&&!String.valueOf(mesBzzFcfhjhDto.getState()).equals("")){
                        int state = mesBzzFcfhjhDto.getState();
                        sql.append(" and a.STATE = #{state, jdbcType=INTEGER}");
                    }
                    if(mesBzzFcfhjhDto.getJixing()!=null&&!String.valueOf(mesBzzFcfhjhDto.getJixing()).equals("")){
                        String jixing = mesBzzFcfhjhDto.getJixing();
                        sql.append(" and a.JIXING = #{jixing, jdbcType=VARCHAR}");
                    }
                    if(mesBzzFcfhjhDto.getTuhao()!=null&&!String.valueOf(mesBzzFcfhjhDto.getTuhao()).equals("")){
                        String tuhao = mesBzzFcfhjhDto.getTuhao();
                        sql.append(" and a.TUHAO like '%'||#{tuhao, jdbcType=VARCHAR}||'%'");
                    }
                    sql.append(" order by a.qualified_label_number ");

                    SELECT(sql.toString());
                }
            }.toString();
        }
    }

    @Update(" update MES_BZZ_FCFHJH " +
            " set SCHEDULED_START_TIME = to_date(#{scheduledStartTime, jdbcType=DATE},'yyyy-mm-dd hh24:mi:ss'),PLANNED_ASSESSMENT_TIME=to_date(#{plannedAssessmentTime, jdbcType=DATE},'yyyy-mm-dd hh24:mi:ss') ,WEIHUREN = #{weihuren ,jdbcType=VARCHAR},WEIHUSHIJIAN=SYSDATE" +
            " where objuid = #{objuid, jdbcType=VARCHAR}")
    int weiHu(MesBzzFcfhjhDto mesBzzFcfhjhDto);

    @Update(" update MES_BZZ_FCFHJH " +
            " set QUHUOREN = #{quhuoren,jdbcType=VARCHAR},QUHUOSHIJIAN=SYSDATE, STATE=1" +
            " where objuid = #{objuid, jdbcType=VARCHAR}")
    int quHuo(MesBzzFcfhjhDto mesBzzFcfhjhDto);

    @Update(" update MES_BZZ_FCFHJH " +
            " set WANCHENGREN = #{wanchengren,jdbcType=VARCHAR},WANCHENGSHIJIAN=SYSDATE, STATE=3" +
            " where objuid = #{objuid, jdbcType=VARCHAR}")
    int chuKu(MesBzzFcfhjhDto mesBzzFcfhjhDto);

    @Update(" update MES_BZZ_FCFHJH " +
            " set WANJIANREN = #{wanjianren,jdbcType=VARCHAR},WANJIANSHIJIAN=SYSDATE" +
            " where objuid = #{objuid, jdbcType=VARCHAR}")
    int wanJian(MesBzzFcfhjhDto mesBzzFcfhjhDto);

    @Update(" update MES_BZZ_FCFHJH " +
            " set RUKUREN = #{rukuren,jdbcType=VARCHAR},RUKUSHIJIAN=SYSDATE" +
            " where objuid = #{objuid, jdbcType=VARCHAR}")
    int ruKu(MesBzzFcfhjhDto mesBzzFcfhjhDto);

    /**
     * 防护计划的入库人入库时间,为计量计划的入库人入库时间
     * @param mesBzzFcfhjhDto
     * @return
     */
    @Update(" update MES_BZZ_FCFHJH " +
            " set RUKUREN = #{rukuren,jdbcType=VARCHAR},RUKUSHIJIAN=SYSDATE" +
            " where QUALIFIED_LABEL_NUMBER = #{qualifiedLabelNumber, jdbcType=VARCHAR}" +
            " and PLAN_TYPE = 1 and STATE = 0")
    int fanghuruku(MesBzzFcfhjhDto mesBzzFcfhjhDto);

    @Select(" select objuid as pgrid,name as pgrname,eid as pgreid,0 as pageNo,10 as pageSize from stf_employee where factory_id =#{factory,jdbcType=VARCHAR} and delete_flag=0")
    List<MesBzzFcfhjhVo> selectEidName(MesBzzFcfhjhDto mesBzzFcfhjhDto);

    @Insert(" insert " +
            "  into mes_bzz_order " +
            "  (OBJUID,BARCODE,DISPATCHER,DISPATCHTIME,PLANUID) " +
            " values " +
            "  (#{objuid,jdbcType=VARCHAR},#{barcode,jdbcType=VARCHAR},#{paigongren,jdbcType=VARCHAR},sysdate,#{planobjuid,jdbcType=VARCHAR}) ")
    int inMbo(MesBzzFcfhjhDto mesBzzFcfhjhDto);
    @Insert(" insert " +
            " into mes_bzz_process " +
            "  (BARCODEOBJUID,OPERATIONNUMBER,OPERATIONNAME,WORKFLOW,MONITOROBJUID,JIANYANYUAN) " +
            " values " +
            "  (#{barcodeobjuid,jdbcType=VARCHAR}, 10,'产品标识确认','确认派工卡片上的信息与产品丶工艺规程。','N/A','N/A') ")
    int inMbp1(MesBzzFcfhjhDto mesBzzFcfhjhDto);
    @Insert(" insert " +
            " into mes_bzz_process " +
            "  (BARCODEOBJUID,OPERATIONNUMBER,OPERATIONNAME,WORKFLOW,JIANYANYUAN) " +
            " values " +
            "  (#{barcodeobjuid,jdbcType=VARCHAR},20,'封存签准备','准备工艺规程','N/A') " )
    int inMbp2(MesBzzFcfhjhDto mesBzzFcfhjhDto);
    @Insert(" insert " +
            " into mes_bzz_process " +
            "  (BARCODEOBJUID,OPERATIONNUMBER,OPERATIONNAME,WORKFLOW,MONITOROBJUID) " +
            " values " +
            "  (#{barcodeobjuid,jdbcType=VARCHAR},30,'产品质量状态检查','产品封存前无损伤检查确认。','N/A') " )
    int inMbp3(MesBzzFcfhjhDto mesBzzFcfhjhDto);
    @Insert(" insert " +
            " into mes_bzz_process " +
            "  (BARCODEOBJUID,OPERATIONNUMBER,OPERATIONNAME,WORKFLOW,MONITOROBJUID) " +
            " values " +
            "  (#{barcodeobjuid,jdbcType=VARCHAR},40,'封存过程确认','封存过程符合工艺规程规定的方法。','N/A') " )
    int inMbp4(MesBzzFcfhjhDto mesBzzFcfhjhDto);
    @Insert(" insert " +
            " into mes_bzz_process " +
            "  (BARCODEOBJUID,OPERATIONNUMBER,OPERATIONNAME,WORKFLOW,MONITOROBJUID) " +
            " values " +
            "  (#{barcodeobjuid,jdbcType=VARCHAR},50,'封存标签确认','核对名称丶图号丶数量。','N/A') " )
    int inMbp5(MesBzzFcfhjhDto mesBzzFcfhjhDto);
    @Insert(" insert " +
            " into mes_bzz_process " +
            "  (BARCODEOBJUID,OPERATIONNUMBER,OPERATIONNAME,WORKFLOW,MONITOROBJUID) " +
            " values " +
            "  (#{barcodeobjuid,jdbcType=VARCHAR},60,'封存质量确认','检查产品封存时无漏气。','N/A')  " )
    int inMbp6(MesBzzFcfhjhDto mesBzzFcfhjhDto);



    @Select(" select to_number(fun_get_order_code(#{factory,jdbcType=VARCHAR})) as BARCODE from dual ")
    int barCodeGenerate(String factory);

    /**
     * 修改派工状态为以派工,添加派工人,派工时间
     */
    @Update("update MES_BZZ_FCFHJH set state = 2, paigongren = #{paigongren,jdbcType=VARCHAR}, paigongshijian=SYSDATE where objuid = #{planobjuid, jdbcType=VARCHAR}")
    int updatepaigong(MesBzzFcfhjhDto mesBzzFcfhjhDto);

    /**
     * 修改order表的操作者为被派工者
     */
    @Update("update mes_bzz_order t set t.operator = #{operAtor, jdbcType=VARCHAR} where t.planuid = #{objuid, jdbcType=VARCHAR} ")
    int updatecaozuozhe(MesBzzOrderDo mesBzzOrderDo);

    /**
     * 生成一条新的防护计划
     */
    @Insert("insert into mes_bzz_fcfhjh\n" +
            "  (objuid,\n" +
            "   QUALIFIED_LABEL_NUMBER,\n" +
            "   EXPIRATION_TIME,\n" +
            "   PLAN_TYPE,\n" +
            "   SEALING_METHOD,\n" +
            "   INTERNAL_DIVISION_OF_LABOR,\n" +
            "   SEAL_TYPE,\n" +
            "   STATE,\n" +
            "   DELIVERY_TIME,\n" +
            "   EX_WAREHOUSE_PERSON,\n" +
            "   WAREHOUSING_TIME,\n" +
            "   WAREHOUSER,\n" +
            "   C_USER,\n" +
            "   C_TIME,\n" +
            "   FACTORY,\n" +
            "   DEL_FLAG,\n" +
            "   MODIFIER,\n" +
            "   MODIFICATION_TIME,\n" +
            "   QUHUOREN,\n" +
            "   QUHUOSHIJIAN,\n" +
            "   PAIGONGHAO,\n" +
            "   JIXING,\n" +
            "   TUHAO,\n" +
            "   JIAOFUCHEJIAN,\n" +
            "   JIESHOUCHEJIAN,\n" +
            "   JIAOFUSHUNXU,\n" +
            "   KUCUN_OBJUID,\n" +
            "   SHULIANG,\n" +
            "   MINGCHENG,\n" +
            "   PAIGONGREN,\n" +
            "   PAIGONGSHIJIAN,\n" +
            "   DW,PC,JC)\n" +
            "  (select sys_guid(),\n" +
            "          QUALIFIED_LABEL_NUMBER,\n" +
            "          EXPIRATION_TIME,\n" +
            "          PLAN_TYPE,\n" +
            "          SEALING_METHOD,\n" +
            "          INTERNAL_DIVISION_OF_LABOR,\n" +
            "          SEAL_TYPE,\n" +
            "          0,\n" +
            "          DELIVERY_TIME,\n" +
            "          EX_WAREHOUSE_PERSON,\n" +
            "          WAREHOUSING_TIME,\n" +
            "          WAREHOUSER,\n" +
            "          C_USER,\n" +
            "          C_TIME,\n" +
            "          FACTORY,\n" +
            "          DEL_FLAG,\n" +
            "          MODIFIER,\n" +
            "          MODIFICATION_TIME,\n" +
            "          QUHUOREN,\n" +
            "          QUHUOSHIJIAN,\n" +
            "          PAIGONGHAO,\n" +
            "          JIXING,\n" +
            "          TUHAO,\n" +
            "          JIAOFUCHEJIAN,\n" +
            "          JIESHOUCHEJIAN,\n" +
            "          JIAOFUSHUNXU,\n" +
            "          KUCUN_OBJUID,\n" +
            "          SHULIANG,\n" +
            "          MINGCHENG,\n" +
            "          PAIGONGREN,\n" +
            "          PAIGONGSHIJIAN,\n" +
            "          DW,PC,JC\n" +
            "     from mes_bzz_fcfhjh\n" +
            "    where objuid = #{objuid, jdbcType=VARCHAR})")
    int insertNewFanghu(MesBzzFcfhjhDto mesBzzFcfhjhDto);

    /**
     * 防护计划完成是生成防护标签,并存入打印表
     */
    @Insert("insert into MES_SS_PRODUCT_PRINTFINFO(objuid,JIXING,TUHAO,NAME,FINFO,PGH,DW,CODE,CODE_USER,C_TYPE,FACTORY_ID,P_NUM)" +
            " values(sys_guid(),#{jixing},#{tuhao},#{mingcheng},#{sealingMethod},#{paigonghao},#{dw}," +
            " 'FHQBH' || to_char(sysdate, 'yyyymmdd') || lpad(seq_fcqbh_code.nextval, 4, 0),#{fanghuuser},'防护签',#{factory},0)")
    int insertprint(MesBzzFcfhjhDto mesBzzFcfhjhDto);
}
